package com.inspectionwork.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inspectionwork.common.base.BaseService;
import com.inspectionwork.model.FireDepartment;
import com.inspectionwork.model.IndustryAuthorities;
import com.inspectionwork.support.model.RespEntity;
import com.inspectionwork.support.utils.requestutil.HttpRequestUtil;
import com.inspectionwork.web.mapper.FireDepartmentMapper;
import com.inspectionwork.web.mapper.IndustryAuthoritiesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yang
 */
@Service
@Slf4j
public class BusinessService extends BaseService {
    @Resource
    private FireDepartmentMapper fireDepartmentMapper;
    @Resource
    private IndustryAuthoritiesMapper industryAuthoritiesMapper;


    /**
     * 消防主管部门树形列表
     * @param jsonParam
     * @return
     */
    public RespEntity fireDepartmentTreeList(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String id = "010107";
        FireDepartment fireDepartment = fireDepartmentMapper.selectById(id);
        List<FireDepartment> orgs = getfireDepartmentTreeList(id);
        fireDepartment.put("orgs",orgs);
        return RespEntity.success(fireDepartment);
    }
    public List<FireDepartment> getfireDepartmentTreeList(String id) {
        List<FireDepartment> list = new ArrayList<>();
        List<FireDepartment> oneList = fireDepartmentMapper.selectList(new QueryWrapper<FireDepartment>().eq("pid", id));
        for (FireDepartment one : oneList) {
            List<FireDepartment> twoList = getfireDepartmentTreeList(one.getId());
            one.put("orgs", twoList);
            list.add(one);
        }
        return list;
    }
    /**
     * 相符部门树形列表
     * @param jsonParam
     * @return
     */
    public RespEntity industryAuthoritiesTreeList(JSONObject jsonParam){
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        String id = "01010D";
        IndustryAuthorities fireDepartment = industryAuthoritiesMapper.selectById(id);
        List<IndustryAuthorities> orgs = getindustryAuthoritiesTreeList(id);
        fireDepartment.put("orgs",orgs);
        return RespEntity.success(fireDepartment);
    }
    public List<IndustryAuthorities> getindustryAuthoritiesTreeList(String id) {
        List<IndustryAuthorities> list = new ArrayList<>();
        List<IndustryAuthorities> oneList = industryAuthoritiesMapper.selectList(new QueryWrapper<IndustryAuthorities>().eq("pid", id));
        for (IndustryAuthorities one : oneList) {
            List<IndustryAuthorities> twoList = getindustryAuthoritiesTreeList(one.getId());
            one.put("orgs", twoList);
            list.add(one);
        }
        return list;
    }
}
